package what.basic.list;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
public class Jcomboboxevent implements ItemListener,ActionListener{
	String[] fs={"12","14","16","18","20"," ","24","26","28"};
	String dm="please select or enter font size!";
	Font font=null;
	JComboBox cmb=null;
	JLabel lbl=null;
	public Jcomboboxevent (){
		JFrame f=new JFrame ("Jcomboboxevent");
		Container cp=f.getContentPane();
		cp.setLayout(new GridLayout(2,1));
		lbl=new JLabel("Swing",JLabel.CENTER);
		font=new Font("SansSerif",Font.PLAIN,12);
		lbl.setFont(font);
		cmb=new JComboBox(fs);
		cmb.setBorder(BorderFactory.createTitledBorder("please select font size"));
		cmb.setEditable(true);
		ComboBoxEditor editor=cmb.getEditor();
		cmb.configureEditor(editor, dm);
		cmb.addItemListener(this);
		cmb.addActionListener(this);
		cp.add(lbl);
		cp.add(cmb);
		f.pack();
		f.show();
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}
public static void main(String[] args){
	new Jcomboboxevent();
	
}
@Override
public void itemStateChanged(ItemEvent e) {
	if(e.getStateChange()==ItemEvent.SELECTED){
		int fs=0;
		try{
			fs=Integer.parseInt((String)e.getItem());
			lbl.setText("Swing current font size:"+ fs);
		}
		catch(NumberFormatException ne){
			
		}
	}
}
public void actionPerformed(ActionEvent e) {
	boolean isf=true;
	int fs=0;
	String t=(String)cmb.getSelectedItem();
	try{
		fs=Integer.parseInt(t);
		for(int i=0;i<cmb.getItemCount();i++){
			if(cmb.getItemAt(i).equals(t)){
				isf=false;
				break;
			}
		}
		if (isf){
			cmb.insertItemAt(t, 0);
		}
		font=new Font("SansSerif",Font.PLAIN,fs);
		lbl.setFont(font);
	}
	catch(NumberFormatException ne){
		cmb.getEditor().setItem("your entered number is not integer");
	}
}

}
